package com.example.njut;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.solver.LinearSystem;
import androidx.constraintlayout.solver.widgets.Helper;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CountDownActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView number;
    private Button start;

    Animation animation;
    Animation animation2;
    int count = 4;
    int num = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count_down);

        number = (TextView) findViewById(R.id.textView10);
        start = (Button) findViewById(R.id.button4);
        animation = AnimationUtils.loadAnimation(this,R.anim.animation_a);
        animation2 = AnimationUtils.loadAnimation(this,R.anim.animation_b);
        start.setOnClickListener(this);


    }
    public int getCount(){
        if(count>1) {
            count--;
        /*if(count<1||count>10){
            count=3;
        }*/
        }
        return count;
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                if(count<=1){
                    number.setText("游戏开始");
                    handler.sendEmptyMessageDelayed(1,0);
                }else{
                    number.setText(""+getCount());
                    handler.sendEmptyMessageDelayed(0,1000);
                    number.startAnimation(animation);
                }
            }else{
                Intent intent = new Intent();
                intent.setClass(CountDownActivity.this,BallActivity.class);
                startActivity(intent);
                CountDownActivity.this.finish();
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (num){
            case 0:
                /* Toast.makeText(CountdownActivity.this,"点击次数为奇数",Toast.LENGTH_SHORT).show();*/
                //handler.removeMessages(1);
                handler.sendEmptyMessageDelayed(0,1000);
                //num = 1;
                break;
            /*case 1:
                //Toast.makeText(CountdownActivity.this,"点击次数为偶数",Toast.LENGTH_SHORT).show();
                handler.removeMessages(0);
                handler.sendEmptyMessageDelayed(1,1000);
                num = 0;
                break;*/
        }
    }
}
